package com.zhang.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageInfo;
import com.zhang.entity.dto.MissionDto;
import com.zhang.entity.vo.*;
import com.zhang.service.TaskService;
import com.zhang.entity.response.ResponseResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * User: 张
 * Date: 2024-11-07
 * Time: 16:39
 * Description:
 */
@RestController
@RequestMapping("/task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    @PostMapping("/queryBugMessage")
    public ResponseResult<Object> queryBugMessage(@RequestBody QueryBugBody queryBugBody,
                                                  @Param("pageNum") Integer pageNum,
                                                  @Param("pageSize") Integer pageSize) {
        return ResponseResult.success(taskService.queryBugMessage(queryBugBody, pageNum, pageSize));
    }

    @PostMapping("/registerBugMessage")
    public ResponseResult<String> registerBugMessage(@RequestPart("file") @RequestParam(required = false) MultipartFile[] filesUpload,
                                                     @RequestPart("bugRegister") BugRegisterBody bugRegister) {
        if (bugRegister.getDeviceId() == null) return ResponseResult.error("设备编号不能为空");
        return taskService.registerBugMessage(filesUpload, bugRegister) ? ResponseResult.success() : ResponseResult.error();
    }

    @GetMapping("/downLoadBugFile")
    public ResponseEntity<byte[]> downLoadBugFile(@Param("fileName") String fileName) {
        return taskService.fileDownload(fileName);
    }

    @PostMapping("/dispatchMission")
    @SaCheckRole("admin")
    public ResponseResult<Object> dispatchMission(@RequestBody DispatchMissionBody dispatchMission) {
        return taskService.dispatchMission(dispatchMission) ? ResponseResult.success() : ResponseResult.error();
    }

    @GetMapping("/getMyBugMission")
    public ResponseResult<Object> getMyBugMission(@Param("pageNum") Integer pageNum,
                                                  @Param("pageSize") Integer pageSize) {
        return ResponseResult.success(
                taskService.getMyBugMission(
                        StpUtil.getLoginIdAsInt(), pageNum, pageSize
                )
        );
    }

    @PostMapping("/completeMission")
    public ResponseResult<String> completeMission(@RequestPart("file") @RequestParam(required = false) MultipartFile[] filesUpload,
                                                  @RequestPart("completeMissionBody") CompleteMissionBody completeMissionBody) {
        return taskService.completeMission(completeMissionBody, filesUpload) ? ResponseResult.success() : ResponseResult.error();
    }

    @GetMapping("/getMissionMessage")
    @SaCheckRole("admin")
    public ResponseResult<Object> getMission(@RequestBody MissionBody missionBody,
                                             @Param("pageSize") Integer pageSize,
                                             @Param("pageNum") Integer pageNum) {
        return ResponseResult.success(taskService.getMission(pageNum, pageSize, missionBody));
    }

    @PostMapping("/changBugVersion")
    @SaCheckRole("admin")
    public ResponseResult<Object> changeBugVersion(@RequestBody BugVersionBody bugVersionBody){
        return taskService.changeBugVersion(bugVersionBody) ? ResponseResult.success() : ResponseResult.error();
    }

}











